package com.jingdianjichi.subject.application.Interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * FeignInterceptor 类
 *
 * @author 熊苏鹏1
 * @date 2025/3/11 12:44
 * @description 远程调用拦截器，重置请求头
 */
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        if (request != null) {
            String loginId = request.getHeader("loginId");
            if(loginId != null){
                requestTemplate.header("loginId", loginId);
            }
        }
    }
}
